<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="家政名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入家政名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="家政分类id" prop="cateId">
        <el-input v-model="queryParams.cateId" placeholder="请输入家政分类id" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="商家id" prop="storeId">
        <el-input v-model="queryParams.storeId" placeholder="请输入商家id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['ds:product:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ds:product:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ds:product:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['ds:product:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="家政图片" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="家政名" align="center" prop="name" />
      <el-table-column label="关键字" align="center" prop="keyword" />
      <el-table-column label="家政分类id" align="center" prop="cateId">
        <template #default="scope">
          {{ parseCategory(scope.row.cateId) }}
        </template>
      </el-table-column>
      <el-table-column label="原价" align="center" prop="price" />
      <el-table-column label="现价" align="center" prop="vipPrice" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="销量" align="center" prop="sales" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <span>{{ scope.row.status == 1 ? "正常" : "删除" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分佣金额" align="center" prop="quota" />
      <el-table-column label="商家名称" align="center" prop="storeId">
        <template #default="scope">
          {{ parseStore(scope.row.storeId) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ds:product:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ds:product:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改家政信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="productRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="家政图片" prop="image">
          <image-upload v-model="form.image" :limit="1" />
        </el-form-item>
        <el-form-item label="家政轮播图" prop="sliderImage">
          <image-upload v-model="form.sliderImage" />
        </el-form-item>
        <el-form-item label="家政名" prop="name">
          <el-input v-model="form.name" placeholder="请输入家政名" />
        </el-form-item>
        <el-form-item label="家政简介" prop="info">
          <Editor v-model="form.info" placeholder="请输入商品简介"></Editor>
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键字" />
        </el-form-item>
        <el-form-item label="家政分类" prop="cateId">
          <el-select placeholder="请选择分类" v-model="form.cateId" clearable>
            <el-option v-for="item in allCategoryList" :key="item.id" :label="item.cateName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原价" prop="price">
          <el-input v-model="form.price" placeholder="请输入原价" />
        </el-form-item>
        <el-form-item label="现价" prop="vipPrice">
          <el-input v-model="form.vipPrice" placeholder="请输入现价" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="销量" prop="sales">
          <el-input v-model.number="form.sales" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model.number="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="浏览量" prop="browse">
          <el-input v-model.number="form.browse" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="分佣金额" prop="quota">
          <el-input v-model="form.quota" placeholder="请输入分佣金额" />
        </el-form-item>
        <el-form-item label="商家" prop="storeId">
          <el-select placeholder="请选择商家" v-model="form.storeId" clearable>
            <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Product">
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/ds/product";
import { listStore } from "@/api/ds/store";
import { listCategory } from "@/api/ds/category";

const { proxy } = getCurrentInstance();

const productList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const storeList = ref([]);

const getStoreList = async () => {
  let res = await listStore();
  if (res.code == 200) {
    storeList.value = res.rows;
  }
};

getStoreList();

const parseStore = (storeId) => {
  if (!storeId) return "";
  const store = storeList.value.find((item) => item.id === storeId);
  return store ? store.name : "";
};

const allCategoryList = ref([]);

const parseCategory = (categoryId) => {
  if (!categoryId) return "";
  const category = allCategoryList.value.find((item) => item.id == categoryId);
  return category ? category.cateName : "";
};

const initAllCategory = async () => {
  let res = await listCategory();
  if (res.code == 200) {
    allCategoryList.value = res.rows;
  }
};

initAllCategory();

const data = reactive({
  form: {
    type: 1,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    image: null,
    tagId: null,
    sliderImage: null,
    name: null,
    info: null,
    keyword: null,
    barCode: null,
    brand: null,
    cateId: null,
    price: null,
    vipPrice: null,
    postage: null,
    presellTime: null,
    sort: null,
    sales: null,
    stock: null,
    status: null,
    isBenefit: null,
    isBest: null,
    isNew: null,
    isPostage: null,
    isSpell: null,
    spellPrice: null,
    isDel: null,
    browse: null,
    origin: null,
    size: null,
    saveWay: null,
    isLimit: null,
    limitNum: null,
    specifications: null,
    realStock: null,
    proDate: null,
    expDate: null,
    shareMsg: null,
    postscript: null,
    quota: null,
    storeId: null,
    type: 1,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询家政信息列表 */
function getList() {
  loading.value = true;
  listProduct(queryParams.value).then((response) => {
    productList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    image: null,
    tagId: null,
    sliderImage: null,
    name: null,
    info: null,
    keyword: null,
    barCode: null,
    brand: null,
    cateId: null,
    price: null,
    vipPrice: null,
    postage: null,
    presellTime: null,
    sort: null,
    sales: null,
    stock: null,
    status: null,
    isBenefit: null,
    isBest: null,
    isNew: null,
    isPostage: null,
    isSpell: null,
    spellPrice: null,
    isDel: null,
    browse: null,
    origin: null,
    size: null,
    saveWay: null,
    isLimit: null,
    limitNum: null,
    specifications: null,
    realStock: null,
    proDate: null,
    expDate: null,
    shareMsg: null,
    postscript: null,
    remark: null,
    createTime: null,
    quota: null,
    storeId: null,
    type: 1,
  };
  proxy.resetForm("productRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加家政信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getProduct(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改家政信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateProduct(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProduct(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除家政信息编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delProduct(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ds/product/export",
    {
      ...queryParams.value,
    },
    `product_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
